package com.da.web.apply_change.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.da.common.Result;
import com.da.web.apply_change.entity.ApplyChange;
import com.da.web.apply_change.entity.ApplyDoParm;
import com.da.web.apply_change.entity.ApplyListParm;
import com.da.web.apply_change.entity.ApplyParm;
import com.da.web.apply_change.service.ApplyChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author da
 * @time 2023/12/13 上午 11:53
 */
@RestController
@RequestMapping("/api/applyChange")
public class ApplyChangeController {
    @Autowired
    private ApplyChangeService applyChangeService;

    //保存申请
    @PostMapping("/applySave")
    public Result applySave(@RequestBody ApplyParm parm) {
        applyChangeService.applySave(parm);
        return Result.success("申请成功");
    }

    @GetMapping("/getList")
    public Result getList(ApplyListParm parm) {
        IPage<ApplyChange> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        //对应的学生能看到自己的申请信息
        if (parm.getUserType().equals("0")) {
            QueryWrapper<ApplyChange> query = new QueryWrapper<>();
            //通过申请人的id获取被交换人的id查询
            query.lambda().eq(ApplyChange::getApplyUserId, parm.getUserId())
                    .or().eq(ApplyChange::getChangeUserId, parm.getUserId());
            IPage<ApplyChange> list = applyChangeService.page(page, query);
            return Result.success("查询成功", list);
        } else {
            IPage<ApplyChange> list = applyChangeService.page(page);
            return Result.success("查询成功", list);
        }
    }

    @PostMapping("/applyDo")
    public Result applyDo(@RequestBody ApplyDoParm change) {
        if (change.getUserType().equals("0")) {
            return Result.error("管理员才能操作");
        }
        applyChangeService.applyDo(change);
        return Result.success("操作成功");
    }
}
